<script setup>
import { PASSWORD_REGEXP } from '@/utils/regExp'
const props = defineProps({
  id: {
    type: String,
    default: ''
  },
  visible: {
    type: Boolean,
    default: false
  }
})
const emit = defineEmits(['update:visible', 'save'])
const formRef = ref(null)
const formData = reactive({
  password: ''
})
const formRules = reactive({
  password: [
    { required: true, trigger: 'blur', message: '请输入密码！' },
    { pattern: PASSWORD_REGEXP, trigger: 'blur', message: '8-16位, 至少包含一位大写字母、小写字母、数字' }
  ]
})
const handleSave = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      submitBtnLoading.value = true
      emit('save', formData)
    } else {
      return false
    }
  })
}
const resetForm = () => {
  emit('update:visible')
  formRef.value?.resetFields()
}
const submitBtnLoading = ref(false)
watch(
  () => props.visible,
  async (newValue, oldValue) => {
    if (newValue) {
      submitBtnLoading.value = false
    }
  }
)
defineExpose({
  submitBtnLoading
})
</script>

<template>
  <el-dialog :close-on-click-modal="false" v-model="visible" title="修改密码" @close="resetForm" width="60%">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="80px">
      <el-form-item prop="password" label="密码">
        <el-input v-model="formData.password" placeholder="请输入密码" clearable />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSave" :loading="submitBtnLoading">确认</el-button>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped></style>
